<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');

$aCamForm = array();
require("_inc/const.campiSetdati.php");

if (reqVar('azione') == 'salva') {
	$msgPag = confMod($aCamForm);
} 
if (reqVar('azione') == '') {
	$sqlSel = "SELECT * FROM rgl_set_dati " .
		" WHERE id_set_dati = ".intval(reqVar('sd'));
	$objDB->exec($sqlSel,'modRec');
	if (!$aRow = $objDB->fetch('modRec')) {
		backOnErr("setDatiMod - sd. '".reqVar('sd')."' mancante o errato");
	}
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			$_REQUEST[$aCX[0]] = $aRow[$aCX[0]];
		}
	}
}
testaHtml('Gestione modelli',$msgPag,'modl');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:document.frm1.submit();" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('sd');?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Set dati bilancio</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:document.frm1.submit();" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
			<?
echo "</form>\n";
codaHtml();
exit();

function confMod($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_set_dati'] = strtoupper(trim(reqVar('cod_set_dati')));
	if (reqVar('cod_set_dati')=='') {
		$msg .= _t("Codice obbligatorio")."<br>";
	}
	if (preg_match("/[^_A-Z0-9]/",reqVar('cod_set_dati'))) {
		$msg = _t("Codice '%s' puo' contenere solo lettere numeri e '-'",reqVar('cod_set_dati'))."<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	$sqlSel = "SELECT id_set_dati FROM rgl_set_dati " 
			." WHERE cod_set_dati = '".reqVarAS('cod_set_dati')."'"
			." AND id_set_dati <> ".reqVarInt('sd');
	$objDB->exec($sqlSel);
	$aRow = $objDB->fetch();
	if ($aRow) {
		$msg .= _t("Codice gia' esistente")."<br>";
	}
	if ($msg != '') {
		return $msg;
	}
			
	$sSql = "UPDATE rgl_set_dati SET cod_set_dati = '".trim(reqVarAS('cod_set_dati'))."'";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			if ($aCX[0]=='cod_set_dati') continue;
			$sSql .= ", ".$aCX[0]." = '".reqVarAS($aCX[0])."' ";
		}
	}
	$sSql .= " WHERE id_set_dati = ".reqVarInt('sd');
	$objDB->exec($sSql);
	
	header("Location: ".urlTorna("Set '".reqVar('cod_set_dati')."' modificato"));
	exit();
}
